<?php
class lb_biz_filter
{
    private $enable_cache = false;
    private $cache_time = 3600;
    private $oRedis;
    const COLLECT_KEY_PREFFIX = 'lmf_';

    function __construct(){
        $this->oRedis = new ml_model_rdsLbFilter();
    }

    public function run($class_id , $tag_ids , $page)
    {
        global $ML_MC_CONFIG;

        
        
        //先看缓存里有没有
        $mc_key = self::COLLECT_KEY_PREFFIX.$class_id.'_';
        sort($tag_ids); //按从小到大排个序尽量使用缓存
        foreach ($tag_ids as $tag_id){
            $mc_key .= ml_tool_lbTag::tagid2str($tag_id);
        }
        $mc_key .= '_'.$page;

        if($this->enable_cache)
        {
            $mc_conf = ml_factory::load_standard_conf('mc');
            $oMc = new Lib_cache_mc();
            $oMc->init_cache(0,4);
            $oMc->connect($mc_conf['lb_filter']);
            $data = $oMc->get($mc_key);
            if(is_array($data))
                return $data;
        }

        //以下基于REDIS取数据
        $arr_picids = array();
        $arr_pics     = array();
        $arr_tgid     = array();
        $arr_albumid = array();
        $arr_album     = array();

        $rs = $this->oRedis->get_filter_result($class_id , $tag_ids , $page);

        //最大页码
        $redisRs = $this->oRedis->get_data();
        $arr_picids = $redisRs['pic_ids'];

        $aResult = array(
            'total' => $redisRs['total'],
            'rows' => array()
        );



        if(count($arr_picids) >= 1)
        {
            //根据ID取图片数据
            $oPhoto = new ml_model_dbLbPhoto();
            $rs = $oPhoto->get_by_pic_id($arr_picids);
            //容错
            $arr_pics = $oPhoto->get_data();

            $oPoint = new ml_model_dbLbPoint();
            $rs = $oPoint->get_by_pointid($arr_picids);
            //容错
            $points = $this->format_point($oPoint->get_data());

            if(count($arr_pics) >= 1)
            {

                //取图片图册对应关系
                $oA2p = new ml_model_dbLbAlbum2pic();
                $rs = $oA2p->get_by_picid($arr_picids);
                $arr_a2p = $this->get_a2p_by_key($oA2p->get_data());
                
                //取出所有tag_id用于取标签信息
                foreach ($arr_pics as $k => $row)
                {
                    if($row['tags_ids'] !="")
                    {
                        $arr_pics[$k]['tgid'] = explode(',' , $row['tags_ids']);
                        $arr_tgid = array_merge($arr_tgid, $arr_pics[$k]['tgid']);
                    }
                    
                    $arr_albumid = array_merge($arr_albumid,$arr_a2p[$row['pic_id']]);
                }
                $arr_tgid = array_unique($arr_tgid);
                $arr_albumid = array_unique($arr_albumid);
                //取图册信息
                $oAlbum = new ml_model_dbLbAlbum();
                $rs = $oAlbum->get_by_ablum_id($arr_albumid);
                //容错
                $arr_album = Tool_array::format_2d_array($oAlbum->get_data() , 'album_id' , Tool_array::FORMAT_FIELD2ROW );
                
                //取tag信息
                $arr_tgid = array_unique($arr_tgid);
                $oTag = new ml_model_dbLbTag();
                $rs = $oTag->get_by_tgid($arr_tgid);
                //容错
                $arr_tag = $this->get_tag_by_tgid($oTag->get_data());
                //格式化数据
                foreach ($arr_pics as $k => $row)
                {
                    $arr_pics[$k]['bimg'] = ml_tool_lbPic2url::pid2url($row['pid'], ML_BG_PICSIZE_ORIGINAL, ML_PICTYPE_LOOKBOOK_PHOTO);
                    $arr_pics[$k]['pic_url'] = ml_tool_lbPic2url::pid2url($row['pid'], ML_BG_PICSIZE_BIG, ML_PICTYPE_LOOKBOOK_PHOTO);
                    $arr_pics[$k]['simg'] = ml_tool_lbPic2url::pid2url($row['pid'], ML_BG_PICSIZE_BIGMIDDLE, ML_PICTYPE_LOOKBOOK_PHOTO);
                    $arr_pics[$k]['point'] = $points[$row['pic_id']];
                    $arr_pics[$k]['id'] = $row['pic_id'];
                    foreach ($arr_a2p[$row['pic_id']] as $aid)
                    {
                        $arr_pics[$k]['albuminfo'][] = $arr_album[$aid];
                    }

                    foreach ($row['tgid'] as $tgid)
                    {
                        $arr_pics[$k]['tag'][$arr_tag[$tgid]['tgid']] = $arr_tag[$tgid]['title'];
                    }
                }

                $aResult['rows'] = $arr_pics;
                
                //存到mc
                if($this->enable_cache)
                {
                    $oMc->set($mc_key , $aResult , 0 , $this->cache_time);
                }
            }//no pics
        }//no pic_id
        //var_dump($aResult);
        return $aResult;
    }

    public function get_a2p_by_key($array){
        if(!is_array($array)) return false;
        $key_array = array();
        foreach($array as $value){
            $key_array[$value['pic_id']][] = $value['album_id'];
        }
        return $key_array;
    }
    public function get_tag_by_tgid($array){
        if(!is_array($array)) return false;
        $tgid_array = array();
        foreach($array as $value){
            $value['tgid'] = ml_tool_lbTag::tgidEncode($value['tagsgroup_id'] , $value['tags_id']);
            $tgid_array[$value['tgid']] = $value;
        }
        return $tgid_array;
        
    }
    public function format_point($array){
        if(!is_array($array)) return false;

        $point_array = array();
        foreach($array as $value){
            $pinfo = json_decode($value['point_info'], true);
            $posi = explode(',', $value['posi']);
            $point['id'] = $row['point_id'];
            $point['title'] = $pinfo['title'];
            $point['brand'] = $pinfo['brand'];
            $point['price'] = $pinfo['price'];
            $point['type'] = $pinfo['type'];
            $point['link'] = $pinfo['link'];
            $point['posi_x'] = $posi[0];
            $point['posi_y'] = $posi[1];
            $point['url'] = ml_tool_lbPic2url::pid2url($value['mark_pid'], ML_BG_PICSIZE_ORIGINAL, ML_PICTYPE_LOOKBOOK_MARK);
            $point_array[$value['pic_id']][] = $point;
            unset($point);
            unset($pinfo);
            unset($posi);
        }
        return $point_array;
    }
    public function getCountByTgid($class_id, $tag_id) {
        return $this->oRedis->getCountByTgid($class_id , $tag_id);
    }
}
?>